package firear.caipiaomonitor;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Calendar;
import java.util.Date;

public class MainActivity extends Activity {
    TextView mTextView = null;
    Button b = null;
    Button []bits = new Button[5];

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            mTextView.setText(LibraryRoomMonitorService.content);
        }
    };

    class BitOnclickLinstener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Button b = (Button) v;
            String text = b.getText().toString();
            if(text.equals("双")){
                b.setText("单");
            }else {
                b.setText("双");
            }
        }
    }

    private int[] getRuleCfg(){
        int[] cfg = new int[5];
        for (int i = 0 ;i < 5; i++){
            if (bits[i].getText().toString().equals("双")){
                cfg[i] = 0;
            }else{
                cfg[i] = 1;
            }
        }
        return cfg;
    }

    private boolean valid(){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
//月
        int month = calendar.get(Calendar.MONTH)+1;
//日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (year > 2020 || month > 7 || day > 1){
            return false;
        }
        return true;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        if(!valid()){
            finish();
            return;
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = findViewById(R.id.button2);



        bits[0] = findViewById(R.id.button);
        bits[1] = findViewById(R.id.button3);
        bits[2] = findViewById(R.id.button4);
        bits[3] = findViewById(R.id.button5);
        bits[4] = findViewById(R.id.button6);

        mTextView = findViewById(R.id.textView2);

        for(Button b : bits){
            b.setOnClickListener(new BitOnclickLinstener());
        }


        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!LibraryRoomMonitorService.monitoring) {
                    int []rule = getRuleCfg();
                    Rule.setRule(rule);
                    LibraryRoomMonitorService.start(MainActivity.this);
                    b.setText("STOP");
                }else {
                    LibraryRoomMonitorService.stop(MainActivity.this);
                    b.setText("START");
                }

            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    handler.sendEmptyMessage(0);
                    SystemClock.sleep(1000);
                }
            }
        }).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(LibraryRoomMonitorService.monitoring){
            b.setText("STOP");
        }else {
            b.setText("START");
        }
    }
}